﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VNPT.Excel;
using System.IO;
using VNPT.Data;

namespace VNPT.DoiSoat.DoiSoat
{
    public partial class u1800_1900DLK : VNPT.Core.uBase
    {
        public u1800_1900DLK()
        {
            InitializeComponent();
        }
       
        List<DoiSoat18001900> li = new List<DoiSoat18001900>();
       
        List<DoiSoat18001900> GetList(string PathExcel, string SheetName)
        {
            li.Clear();
            FileStream stream = File.Open(PathExcel, FileMode.Open);
            IExcelDataReader excelReader;
            if (PathExcel.EndsWith(".xls"))
            {
                excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
            }
            else
            {
                if (PathExcel.EndsWith(".xlsx"))
                {
                    excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("File không hợp lệ!");
                    return null;
                }
            }


            excelReader.IsFirstRowAsColumnNames = true;
            DataSet result = excelReader.AsDataSet();
            foreach (var item in result.Tables[SheetName].Rows)
            {
                var dt = ((DataRow)item);

                int phut = 0;
                int.TryParse((dt[1] ?? "0").ToString(), out phut);

                int dongia = 0;
                int.TryParse((dt[2] ?? "0").ToString(), out dongia);

                int thanhtien = 0;
                int.TryParse((dt[3] ?? "0").ToString(), out thanhtien);

                string DausoDep = System.Text.RegularExpressions.Regex.Replace(dt[0].ToString(), @"x+", "%");

                if (!DausoDep.EndsWith("%"))
                {
                    DausoDep += "%";
                }

                string table = string.Format("Select SUM(CEIL(tg_goi/60)) phut from DAKLAK.ctdt_{0}{1:D2}01@daklak_3  where bigoi LIKE '{2}'",
                    DoiSoatSetting.NAM, DoiSoatSetting.THANG, DausoDep);

                var it = new DoiSoat18001900()
                {
                    Cuoc = 0,
                    Cuoc_DLK = 0,
                    DauSo = dt[0].ToString(),
                    DonGia = dongia,
                    GhiChu = "",
                    Phut = phut,
                    Phut_DLK = 0,
                    Query = table,
                    TrangThai = ""
                };
                li.Add(it);
            }
            return li;
        }
      
        private List<string> GetSheetName(string Path)
        {
            List<string> kq = new List<string>();
            if (System.IO.File.Exists(Path))
            {
                FileStream stream = File.Open(Path, FileMode.Open);//, FileMode.Open, FileAccess.Read);
                IExcelDataReader excelReader;
                if (Path.EndsWith(".xls"))
                {
                    excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                }
                else
                {
                    if (Path.EndsWith(".xlsx"))
                    {
                        excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                    }
                    else
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("File không hợp lệ!");
                        return null;
                    }
                }

                excelReader.IsFirstRowAsColumnNames = true;
                DataSet result = excelReader.AsDataSet();

                foreach (var item in result.Tables)
                {
                    kq.Add(((DataTable)item).TableName);
                }
                return kq;
            }
            return null;
        }
       
        private void btnGoiDi_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void btnLoad_Click(object sender, EventArgs e)
        {

        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            new VNPT.Utility.Grid.GridExport().Export(myGridControl1);
        }

        private void btnTinhToan_Click(object sender, EventArgs e)
        {

        }
    }
}
